package com.bj58.groupbuy.action.templateParse;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bj58.groupbuy.action.model.IRColumns;
import com.bj58.groupbuy.action.model.IRReport;
import com.bj58.groupbuy.action.model.IRSearchConfig;
import com.bj58.groupbuy.action.model.Menu;
import com.bj58.groupbuy.action.service.IRColumnsService;
import com.bj58.groupbuy.action.service.IRReportService;
import com.bj58.groupbuy.action.service.IRSearchConfigService;
import com.bj58.groupbuy.action.service.MenuService;
import com.bj58.groupbuy.action.util.MyPathUtil;
import com.bj58.groupbuy.action.util.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;

public class IRTemplateParse {
	private IRReportService iRReportService = ServiceLocator.get(IRReportService.class);
	private IRSearchConfigService iRSearchConfigService = ServiceLocator.get(IRSearchConfigService.class);
	private MenuService menuService = ServiceLocator.get(MenuService.class);
	private IRColumnsService iRColumnsService = ServiceLocator.get(IRColumnsService.class);
	
	static String templateMainPath = MyPathUtil.getIRPath()+"templateMain.html"; //模板主体
	static String templateSelectOnePath = MyPathUtil.getIRPath()+"templateSelectOne.html";//查询模板 类型1
	static String templateTableOnePath = MyPathUtil.getIRPath()+"templateTableOne.html";//展示模板 类型1
	static String cateSelectPath = MyPathUtil.getIRPath()+"cateSelect.html";//品类级联组件模板
	static String citySelectPath = MyPathUtil.getIRPath()+"citySelect.html";//城市级联组件模板
	static String dataPermissionsPath = MyPathUtil.getIRPath()+"dataPermissions.html";//数据资源权限模板
	static String infoSourcePath = MyPathUtil.getIRPath()+"sourceSelects.html";//信息/流量来源级联模板
	static String chrSelectPath = MyPathUtil.getIRPath()+"chinaHRSelects.html";//中华英才组件模块
	
	public synchronized   void createNewTemplate(String pathId)throws Exception{
		try{
			int menuId = Integer.parseInt(pathId.split("_")[0]);
			//模板主体
			Menu menu = menuService.findById(Menu.class, menuId);
			String mainTemplate = createMain(menu,pathId);
			
			
			IRReport  irReport = iRReportService.getReportListByMenuId(menuId);
			List<IRSearchConfig>  searchConfigList =   iRSearchConfigService.getListByIRid(irReport.getId());
			List<IRColumns>  colsList = iRColumnsService.getColsByReportId(irReport.getId());
			
			//查询模板    和  表格模板
			String searchTemplate = "";
			String tableTemplate = "";
			
			// type 1.查询指标全显示   2.查询指标勾选式
			searchTemplate = createSelectOne(searchConfigList,colsList,irReport);
			tableTemplate = createTableOne();
			String result = mainTemplate;
			result = result.replace("^select^", searchTemplate);
			result = result.replace("^table^", tableTemplate);
			//生成新模板文件
			String filepath =MyPathUtil.getIRPath()+pathId+".html";
			File sf = new File(filepath);
			if(sf.exists()){
				sf.delete();
			}
			RandomAccessFile raf = new RandomAccessFile(sf, "rw");
			raf.seek(raf.length());
			raf.write(result.getBytes("UTF-8"));	
			raf.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	private String createMain(Menu menu,String pathId)throws Exception{
		Map<String,Object> conMap = new HashMap<String,Object>();
		conMap.put("tableName", menu.getName());  //表名
		conMap.put("pathId", pathId);//编号  构成form提交路径
		
		String main = MyPathUtil.getTemByPath(templateMainPath);
		String content  = VelocityTools.render(main, conMap);
		content = content.replace("#!", "$!");
		content = content.replace("&^", "#");
		return content;
	}
	
	
	private String createTableOne()throws Exception{
		String content = MyPathUtil.getTemByPath(templateTableOnePath);
		return content;
	}
	
	private String createSelectOne(List<IRSearchConfig>  searchConfigList , List<IRColumns>  colsList, IRReport irReport)throws Exception{
		Map<String,Object> conMap = new HashMap<String,Object>();
		
		int listSize = 1; //查询模块高度指标  默认为1   typeId为5、6、7则4个为一行  增加高度
		int cateSelectSize = 0; //品类级联查询条件计数
		int citySelectSize = 0; //城市级联查询条件计数
		int selectSize = 0; //总级联查询条件计数
		
		List<IRSearchConfig> searchList = new ArrayList<IRSearchConfig>();  //单字段模糊查询、单字段精准查询、单字段下拉框 三种查询的集合
	
		String cateSelectValue  = "";//品类级联模板内容
		String citySelectValue  = "";//城市级联模板内容
		String infoSourceSelectValue  = "";//信息来源级联模板内容
		String flowSourceSelectValue  = "";//流量来源级联模板内容
		String jxedtFlowSourceSelectValue  = "";//驾校一点通流量来源级联模板内容
		String chrFlowSourceSelectValue  = "";//中华英才流量来源级联模板内容
		String chrIndustrySelectValue  = "";//中华英才行业级联模板内容
		String chrJobSelectValue  = "";//中华英才职位级联模板内容
		
		
		for(IRSearchConfig vo : searchConfigList ){
			int typeId = vo.getSearchTypeId();
			switch (typeId) {
			case 1:
				conMap.put("bcList", 1);
				break;
			case 2:
				conMap.put("cityList", 1);
				break;
			case 3:
				conMap.put("timeLine", 1);
				break;
			case 4:
				conMap.put("timePoint", 1);
				break;
			case 5:
				searchList.add(vo);
				break;
			case 6:
				searchList.add(vo);
				break;
			case 7:
				if(vo.getDetailValue().contains("|")){
					String tempDV = vo.getDetailValue();
					vo.setDetailValue(tempDV.substring(0,tempDV.indexOf("|")));
				}
				searchList.add(vo);
				break;
			case 8:
				conMap.put("groupBy", vo);
				String groupBy=vo.getDetailValue();
				boolean hasStatDate=groupBy.contains("stat_date")||groupBy.contains("statDate")||groupBy.contains("vDate");
				conMap.put("hasStatDate", hasStatDate);
				conMap.put("groupByCount", vo.getDetailValue().split(";").length);
				listSize ++;
				break;
			case 9: //展现品类级联
				conMap.put("cateSelect", 1);
				cateSelectValue = dealSelect(vo);
				cateSelectSize = cateSelectValue.split("</select>").length - 1;
				break;
			case 10://归属品类级联
				conMap.put("cateSelect", 1);
				cateSelectValue = dealSelect(vo);
				cateSelectSize = cateSelectValue.split("</select>").length - 1;
				break;
			case 11: //展现城市级联
				conMap.put("citySelect", 1);
				citySelectValue = dealSelect(vo);
				citySelectSize = citySelectValue.split("</select>").length - 1;
				break;
			case 12://归属城市级联
				conMap.put("citySelect", 1);
				citySelectValue = dealSelect(vo);
				citySelectSize = citySelectValue.split("</select>").length - 1;
				break;
			case 13://图表
				conMap.put("hasChart", 1);
				listSize ++;
				break;
			case 14://数据资源权限
				String dpValues = dataPermissionsDeal(vo);
				conMap.put("dpValues", dpValues);
				listSize ++;
				break;
			case 15://同比、环比
				conMap.put("compare", 1);
				listSize ++;
				break;
			case 17: //展现品类级联 - new
				conMap.put("cateSelect", 1);
				cateSelectValue = dealSelect(vo);
				cateSelectSize = cateSelectValue.split("</select>").length - 1;
				break;
			case 18://归属品类级联 - new 
				conMap.put("cateSelect", 1);
				cateSelectValue = dealSelect(vo);
				cateSelectSize = cateSelectValue.split("</select>").length - 1;
				break;
			case 19://信息来源组件
				conMap.put("infoSourceSelect", 1);
				infoSourceSelectValue = dealSelect(vo);
				listSize ++;
				break;
			case 20://流量来源组件
				conMap.put("flowSourceSelect", 1);
				flowSourceSelectValue = dealSelect(vo);
				listSize ++;
				break;
			case 21://驾校一点通流量来源组件
				conMap.put("jxedtFlowSourceSelect", 1);
				jxedtFlowSourceSelectValue = dealSelect(vo);
				listSize ++;
				break;
			case 22://中华英才流量来源组件
				conMap.put("chrFlowSourceSelectValue", 1);
				chrFlowSourceSelectValue = dealSelect(vo);
				listSize ++;
				break;
			case 23://中华英才行业组件
				conMap.put("chrIndustrySelectValue", 1);
				chrIndustrySelectValue = dealSelect(vo);
				listSize ++;
				break;
			case 24://中华英才职位组件
				conMap.put("chrJobSelectValue", 1);
				chrJobSelectValue = dealSelect(vo);
				listSize ++;
				break;
			}
			
		}
		
		listSize = listSize+(searchList.size() /4);
		if(searchList.size() % 4 > 0 ){
			listSize++;
		}
		//根据品类、城市级联查询条件计数  来 判定行数  并将内容替换
		if(cateSelectSize > 0 && citySelectSize == 0){
			conMap.put("cateSelectValue", cateSelectValue);
			listSize++;
		}else if(cateSelectSize == 0 && citySelectSize > 0){
			conMap.put("citySelectValue", citySelectValue);
			listSize++;
		}else if(cateSelectSize > 0 && citySelectSize > 0){
			selectSize = cateSelectSize + citySelectSize;
			if(selectSize > 5){
				listSize = listSize + 2;
			}else{
				listSize = listSize + 1;
			}
			conMap.put("citySelectValue", citySelectValue);
			conMap.put("cateSelectValue", cateSelectValue);
		}
		conMap.put("infoSourceSelectValue", infoSourceSelectValue);
		conMap.put("flowSourceSelectValue", flowSourceSelectValue);
		conMap.put("jxedtFlowSourceSelectValue", jxedtFlowSourceSelectValue);
		conMap.put("chrFlowSourceSelectValue", chrFlowSourceSelectValue);
		conMap.put("chrIndustrySelectValue", chrIndustrySelectValue);
		conMap.put("chrJobSelectValue", chrJobSelectValue);
		
		//报表模板类型 2   查询指标勾选式  -  指标选项渲染
		Map<String,ArrayList<IRColumns>> tabMap = new HashMap<String,ArrayList<IRColumns>>();
		List<String> tabNameList = new ArrayList<String>();
		int templateType = irReport.getTemplateType();
		if(templateType == 2){
			if("groupBy".equals(colsList.get(0).getEname())){
				colsList.remove(0);
			}
			for(IRColumns col:colsList){
				String tabType = col.getTabType();
				if(tabType==null || "".equals(tabType.trim()))
					tabType="指标";
				ArrayList<IRColumns> tabCols=new ArrayList<IRColumns>();
				if(tabMap.containsKey(tabType))
					tabCols = tabMap.get(tabType);
				else{ //该标签名第一次出现
					tabNameList.add(tabType);
				}
				tabCols.add(col);
				tabMap.put(tabType, tabCols);
				
			}
			conMap.put("tabNameList", tabNameList);
			conMap.put("tabMap", tabMap);
			listSize++;
			int a = colsList.size() / 8;
			int b = colsList.size() % 8;
			listSize = listSize + a;
			if(b > 0){
				listSize++;
			}
		}
		
		conMap.put("listSize", listSize);
		conMap.put("searchList", searchList);
		
		//是否支持跨月
		conMap.put("isMonthSearch", irReport.getIsMonthSearch());
		
		String main = MyPathUtil.getTemByPath(templateSelectOnePath);
		String content  = VelocityTools.render(main, conMap);
		content = content.replace("#!", "$!");
		content = content.replace("&^", "#");
		return content;
	}
	

	
	
	/**级联组件内容获取
	 * @param vo
	 * @return
	 */
	private String dealSelect(IRSearchConfig vo)throws Exception{
		String result = "";
		Map<String,Object> selectMap = new HashMap<String,Object>();
		//获取级联层数
		String show= "";
		for(String temp : vo.getDetailValue().split(";")){
			if(temp.split(":")[0].equals("level")){
				show = temp.split(":")[1];
			}
		}
		String path = "";
		if(vo.getSearchTypeId()  == 9){ //展现品类级联
			selectMap.put("showDP", 1);
			if("1".equals(show)){
				selectMap.put("showDPOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showDPOne", 1);
				selectMap.put("showDPTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showDPOne", 1);
				selectMap.put("showDPTwo", 1);
				selectMap.put("showDPThree", 1);
			}
			path = cateSelectPath;
		}else if(vo.getSearchTypeId()  == 10){//归属品类级联
			selectMap.put("showBC", 1);
			if("1".equals(show)){
				selectMap.put("showBCOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showBCOne", 1);
				selectMap.put("showBCTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showBCOne", 1);
				selectMap.put("showBCTwo", 1);
				selectMap.put("showBCThree", 1);
			}
			path = cateSelectPath;
		}else if(vo.getSearchTypeId()  == 11){//展现城市级联
			selectMap.put("showDL", 1);
			if("1".equals(show)){
				selectMap.put("showDLOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showDLOne", 1);
				selectMap.put("showDLTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showDLOne", 1);
				selectMap.put("showDLTwo", 1);
				selectMap.put("showDLThree", 1);
			}
			path = citySelectPath;
		}else if(vo.getSearchTypeId()  == 12){//归属城市级联
			selectMap.put("showBL", 1);
			if("1".equals(show)){
				selectMap.put("showBLOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showBLOne", 1);
				selectMap.put("showBLTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showBLOne", 1);
				selectMap.put("showBLTwo", 1);
				selectMap.put("showBLThree", 1);
			}
			path = citySelectPath;
		}else if(vo.getSearchTypeId()  == 17){ //展现品类级联 new
			selectMap.put("showNewDP", 1);
			if(!vo.getSename().contains("null")){
				selectMap.put("showNewDPBusinessType", 1);
			}
			
			if("1".equals(show)){
				selectMap.put("showNewDPOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showNewDPOne", 1);
				selectMap.put("showNewDPTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showNewDPOne", 1);
				selectMap.put("showNewDPTwo", 1);
				selectMap.put("showNewDPThree", 1);
			}
			path = cateSelectPath;
		}else if(vo.getSearchTypeId()  == 18){//归属品类级联 new
			selectMap.put("showNewBC", 1);
			if(!vo.getSename().contains("null")){
				selectMap.put("showNewBCBusinessType", 1);
			}
			
			if("1".equals(show)){
				selectMap.put("showNewBCOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showNewBCOne", 1);
				selectMap.put("showNewBCTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showNewBCOne", 1);
				selectMap.put("showNewBCTwo", 1);
				selectMap.put("showNewBCThree", 1);
			}
			path = cateSelectPath;
		}else if(vo.getSearchTypeId()  == 19){//信息来源组件
			selectMap.put("showInfoSelects", 1);
			if("1".equals(show)){
				selectMap.put("showInfoPlatform", 1);
			}else if("2".equals(show)){
				selectMap.put("showInfoPlatform", 1);
				selectMap.put("showInfoSource", 1);
			}
			path = infoSourcePath;
		}else if(vo.getSearchTypeId()  == 20){//流量来源组件
			selectMap.put("showFlowSourceSelects", 1);
			if("1".equals(show)){
				selectMap.put("showFlowSourceOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showFlowSourceOne", 1);
				selectMap.put("showFlowSourceTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showFlowSourceOne", 1);
				selectMap.put("showFlowSourceTwo", 1);
				selectMap.put("showFlowSourceThree", 1);
			}else if("4".equals(show)){
				selectMap.put("showFlowSourceOne", 1);
				selectMap.put("showFlowSourceTwo", 1);
				selectMap.put("showFlowSourceThree", 1);
				selectMap.put("showFlowSourceFour", 1);
			}
			
			path = infoSourcePath;
		}else if(vo.getSearchTypeId()  == 21){//驾校一点通流量来源组件
			selectMap.put("showJxedtSourceSelects", 1);
			if("1".equals(show)){
				selectMap.put("showJxedtSourceOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showJxedtSourceOne", 1);
				selectMap.put("showJxedtSourceTwo", 1);
			}
			
			path = infoSourcePath;
		}else if(vo.getSearchTypeId()  == 22){//中华英才流量来源组件
			selectMap.put("showCHRSourceSelects", 1);
			if("1".equals(show)){
				selectMap.put("showCHRSourceOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showCHRSourceOne", 1);
				selectMap.put("showCHRSourceTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showCHRSourceOne", 1);
				selectMap.put("showCHRSourceTwo", 1);
				selectMap.put("showCHRSourceThree", 1);
			}else if("4".equals(show)){
				selectMap.put("showCHRSourceOne", 1);
				selectMap.put("showCHRSourceTwo", 1);
				selectMap.put("showCHRSourceThree", 1);
				selectMap.put("showCHRSourceFour", 1);
			}
			
			path = chrSelectPath;
		}else if(vo.getSearchTypeId()  == 23){//中华英才行业组件
			selectMap.put("showCHRIndustrySelects", 1);
			if("1".equals(show)){
				selectMap.put("showCHRIndustryOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showCHRIndustryOne", 1);
				selectMap.put("showCHRIndustryTwo", 1);
			}
			path = chrSelectPath;
		}else if(vo.getSearchTypeId()  == 24){//中华英才职位组件
			selectMap.put("showCHRJobSelects", 1);
			if("1".equals(show)){
				selectMap.put("showCHRJobOne", 1);
			}else if("2".equals(show)){
				selectMap.put("showCHRJobOne", 1);
				selectMap.put("showCHRJobTwo", 1);
			}else if("3".equals(show)){
				selectMap.put("showCHRJobOne", 1);
				selectMap.put("showCHRJobTwo", 1);
				selectMap.put("showCHRJobThree", 1);
			}
			path = chrSelectPath;
		}
	
		result = MyPathUtil.getTemByPath(path);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
	
	private String  dataPermissionsDeal (IRSearchConfig vo) throws Exception{
		int dpType = Integer.parseInt(vo.getDetailValue());
		Map<String,Object> selectMap = new HashMap<String,Object>();
		switch (dpType) {
		case 1: //联盟数据品类
			selectMap.put("allianceCate", 1);
			break;
		}
		
		String result = MyPathUtil.getTemByPath(dataPermissionsPath);
		result  = VelocityTools.render( result, selectMap);
		return result;
	}
	
}
